package com.future.controller;


import com.future.domain.DTO.QuestionnaireDTO;
import com.future.domain.Result;
import com.future.domain.vo.QuestionnaireVO;
import com.future.service.IAnswerSheetService;
import com.future.service.IQuestionnaireService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;


@RestController
@RequestMapping("/teacher")
@Api(tags = "教师端问卷管理")
public class QuestionnaireControllerForT {
    @Resource
    private IQuestionnaireService questionnaireService;

    @Resource
    private IAnswerSheetService answerSheetService;

    @PostMapping("/publish")
    @ApiOperation("发布调查问卷")
    @PreAuthorize("hasAuthority('psychologist:questionnaire:publish') or hasAuthority('teacher:questionnaire:publish')")
    public Result add(@RequestBody QuestionnaireDTO questionnaire) {
        return questionnaireService.publish(questionnaire);
    }


    @PutMapping("/update")
    @ApiOperation("更新问卷")
    @PreAuthorize("hasAuthority('psychologist:questionnaire:publish') or hasAuthority('teacher:questionnaire:publish')")
    public Result update(@RequestBody QuestionnaireVO questionnaire) {
        return questionnaireService.updateQuestionnaire1(questionnaire);
    }




    @GetMapping("/get/{questionnaireId}")
    @ApiOperation("获取问卷信息")
    public Result getQuestionnaire(@PathVariable String questionnaireId) {
        return questionnaireService.getQuestionnaire(questionnaireId);
    }



    @GetMapping("/draft/{pageNum}/{pageSize}")
    @ApiOperation("获取草稿")
    public Result draft(@PathVariable("pageNum") Integer pageNum,
                        @PathVariable("pageSize") Integer pageSize
                        ) {
        return questionnaireService.getDraft(pageNum,pageSize);
    }


    @GetMapping("/share")
    @ApiOperation("/生成问卷访问链接")
    public Result share(Long questionnaireId) throws UnsupportedEncodingException {
        return questionnaireService.saveLink(questionnaireId);
    }

    @GetMapping("/getQuestionnaireByLink")
    @ApiOperation("访问问卷")
    public Result getQuestionnaireByLink(@RequestParam("link") String link) throws UnsupportedEncodingException {
        return questionnaireService.getByLink(link);
    }

    @GetMapping("/published/{pageNum}/{pageSize}")
    @ApiOperation("获取已发布的问卷列表")
    public Result getPublished(@PathVariable Integer pageNum,
                               @PathVariable Integer pageSize,
                               @RequestParam(required = false)String title) {
        return questionnaireService.getPublished(pageNum,pageSize,title);
    }


    @GetMapping("/single/{questionnaireId}/{pageNum}/{pageSize}")
    @ApiOperation("单个问卷 ：学生作答详情")
    public Result single(@PathVariable("questionnaireId") Long id,@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
        return answerSheetService.getSingle(pageNum,pageSize,id);
    }

    @GetMapping("/detail/{questionnaireId}")
    @ApiOperation("单个问卷 ：学生作答详情")
    public Result detail(@PathVariable("questionnaireId") Long id) {
        return answerSheetService.selectOne(id);
    }

    @PutMapping("/suggest/{answerSheetId}")
    @ApiOperation("给学生建议")
    @PreAuthorize("hasAuthority('psychologist:questionnaire:publish') or hasAuthority('teacher:questionnaire:publish')")
    public Result suggest(@PathVariable("answerSheetId") Long id,
                          @RequestParam String suggestion) {
        return answerSheetService.updateByT(id,suggestion);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除问卷")
    @PreAuthorize("hasAuthority('psychologist:questionnaire:publish') or hasAuthority('teacher:questionnaire:publish')")
    public Result delete(@PathVariable("id") Long id) {
        return questionnaireService.deleteById(id);
    }







}
